//
//  Geocoder.m
//  SEIO2012
//
//  Created by Gonzalo Abella Dago on 28/05/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Geocoder.h"

@implementation Geocoder

@synthesize delegate;
@synthesize address;
@synthesize placemark;
@synthesize querying;


- (id)initWithAddress:(NSString *)_address
{
	self = [super init];
	if (self) {
		[self setAddress:_address];
	}
	return self;
}



- (void)start
{
	/*
	NSString *baseURL = [[NSString alloc] initWithString:@"http://maps.google.com/maps/api/geocode/"];
	NSString *output = [[NSString alloc] initWithString:@"xml"];
	NSString *parAddress = [[NSString alloc] initWithString:@"C/ Playa del Rompido 28, Boadilla del Monte"];
	NSString *parRegion = [[NSString alloc] initWithString:@"es"];
	NSString *parSensor = [[NSString alloc] initWithString:@"true"];
	NSURL *url = [[[NSURL alloc] initWithString:baseURL
											  +output
											  +@"?"
											  +@"address="
											  +parAddress
											  +@"&"
											  +@"sensor="
											  +parSensor]];
	
	
	[baseURL release];
	[output release];
	[parAddress release];
	[parRegion release];
	[parSensor release];
	*/
	
	NSURL *url = [[NSURL alloc] initWithString:@"http://maps.google.com/maps/api/geocode/json?address=Playa+del+rompido,+boadilla+del+monte&sensor=true&region=es"];
	// Create the request.
	NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];
	// create the connection with the request
	// and start loading the data
	NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
	if (connection) {
		// Create the NSMutableData to hold the received data.
		// receivedData is an instance variable declared elsewhere.
		receivedData = [[NSMutableData data] retain];
	} else {
		// Inform the user that the connection failed.
	}
	
	[url release];
	[request release];
}


- (void)cancel
{
	
}

#pragma mark -
#pragma mark NSConnectionDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.
	
    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
	
    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];
	
    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
	
    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

@end
